﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace DVDFilmoteka.src
{
    /// <summary>
    /// Prepravka k serializaci dat sablobny
    /// </summary>
    public class MediaTemplate
    {
        public String medium { get; set; }
        public String quality { get; set; }
        public String compression { get; set; }
        public String container { get; set; }
        public String resolution { get; set; }
        public String size { get; set; }
        public String language { get; set; }
        public String channelCount { get; set; }
    }

    public class TemplateManager
    {
        private static TemplateManager instance = new TemplateManager();

        public static TemplateManager Instance 
        { 
            get 
            {
                return instance;
            } 
        }

        private Dictionary<String, MediaTemplate> templates = new Dictionary<String, MediaTemplate>();        

        /// <summary>
        /// Konstruktor
        /// </summary>
        private TemplateManager()
        {
            ReloadTemplates();
        }

        /// <summary>
        /// Nacte sablony ze souboru
        /// </summary>
        public void ReloadTemplates()
        {
            String path = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\" + Form1Servant.AssemblyName + "\\templates";

            if (!Directory.Exists(path))
            {
                try 
                {
                    Directory.CreateDirectory(path);
                }
                catch 
                {
                    //MessageBox.Show("Nelze vytvořit adresář pro uložení šablon.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            templates.Clear();

            String[] files = Directory.GetFiles(path, "*.xml");

            MediaTemplate defaultTemplate = new MediaTemplate();
            defaultTemplate.compression = "Nevyplněno";
            defaultTemplate.container = "Nevyplněno";
            defaultTemplate.channelCount = "Nevyplněno";
            defaultTemplate.language = "Nevyplněno";
            defaultTemplate.medium = "Nevyplněno";
            defaultTemplate.quality = "Nevyplněno";
            defaultTemplate.resolution = "Nevyplněno";
            defaultTemplate.size = "Nevyplněno";
            templates.Add("default", defaultTemplate);

            if (files != null && files.Length > 0)
            {
                foreach (String item in files)
                {
                    try
                    {
                        String name = item.Substring(0, item.Length - 4);
                        if (name.Contains("\\")) name = name.Substring(name.LastIndexOf("\\") + 1);

                        if (name != "default")
                        {
                            MediaTemplate _template = new MediaTemplate();
                            XmlSerializer serializer = new XmlSerializer(typeof(MediaTemplate));
                            _template = (MediaTemplate)serializer.Deserialize(new StreamReader(item));

                            if (Validate(_template)) templates.Add(name, _template);
                        }
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// Zvaliduje novou sablonu
        /// </summary>
        /// <returns></returns>
        private Boolean Validate(MediaTemplate template)
        {
            if (String.IsNullOrEmpty(template.compression)) return false;
            if (String.IsNullOrEmpty(template.container)) return false;
            if (String.IsNullOrEmpty(template.channelCount)) return false;
            if (String.IsNullOrEmpty(template.language)) return false;
            if (String.IsNullOrEmpty(template.medium)) return false;
            if (String.IsNullOrEmpty(template.quality)) return false;
            if (String.IsNullOrEmpty(template.size)) return false;
            if (template.resolution == null) return false;

            return true;
        }

        /// <summary>
        /// Ulozi sablony
        /// </summary>
        private void SaveTemplates()
        {
            String path = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\" + Form1Servant.AssemblyName + "\\templates";

            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch
                {
                    MessageBox.Show("Nelze vytvořit adresář pro uložení šablon.", Form1Servant.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            foreach (String item in templates.Keys)
            {
                if (item != "default")
                {
                    MediaTemplate _template = GetTemplateByName(item);

                    try
                    {
                        StreamWriter stream = new StreamWriter(path + "\\" + item + ".xml");

                        XmlSerializer serializer = new XmlSerializer(typeof(MediaTemplate));
                        serializer.Serialize(stream, _template);
                        stream.Close();
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// Vrati seznam dostupnych sablon
        /// </summary>
        public List<String> AvailableTemplateNames
        {
            get
            {
                return templates.Keys.ToList();
            }
        }

        /// <summary>
        /// Vrati sablonu podle jmena
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public MediaTemplate GetTemplateByName(String name)
        {
            MediaTemplate returnTemplate = new MediaTemplate();

            try
            {
                returnTemplate = templates[name];
            }
            catch { }

            if (returnTemplate == null) returnTemplate = templates["default"];

            return returnTemplate;
        }

        /// <summary>
        /// Vymaze sablonu
        /// </summary>
        /// <param name="name"></param>
        public void DeleteTemplateByName(String name)
        {
            String path = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\" + Form1Servant.AssemblyName + "\\templates";

            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch
                {
                    //MessageBox.Show("Nelze vytvořit adresář pro uložení šablon.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                File.Delete(path + "\\" + name + ".xml");
            }

            ReloadTemplates();
        }

        /// <summary>
        /// Prida sablonu
        /// </summary>
        /// <param name="name"></param>
        /// <param name="template"></param>
        public void AddOrUpdateTemplate(String name, MediaTemplate template)
        {
            if (Validate(template))
            {
                if (templates.Keys.Contains(name))
                {
                    templates[name] = template;
                } else {
                    templates.Add(name, template);
                }

                SaveTemplates();
            }
        }
    }
}
